{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install the SpeechRecognition and pipwin classes to work with the Recognizer() class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting SpeechRecognition\n",
      "  Downloading SpeechRecognition-3.8.1-py2.py3-none-any.whl (32.8 MB)\n",
      "Installing collected packages: SpeechRecognition\n",
      "Successfully installed SpeechRecognition-3.8.1\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pipwin\n",
      "  Downloading pipwin-0.5.1.tar.gz (8.8 kB)\n",
      "Collecting docopt\n",
      "  Downloading docopt-0.6.2.tar.gz (25 kB)\n",
      "Requirement already satisfied: requests in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from pipwin) (2.22.0)\n",
      "Collecting pyprind\n",
      "  Downloading PyPrind-2.11.3-py2.py3-none-any.whl (8.4 kB)\n",
      "Requirement already satisfied: six in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from pipwin) (1.14.0)\n",
      "Collecting beautifulsoup4>=4.9.0\n",
      "  Downloading beautifulsoup4-4.9.3-py3-none-any.whl (115 kB)\n",
      "Collecting js2py\n",
      "  Downloading Js2Py-0.71-py3-none-any.whl (1.0 MB)\n",
      "Requirement already satisfied: packaging in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from pipwin) (20.1)\n",
      "Collecting pySmartDL>=1.3.1\n",
      "  Downloading pySmartDL-1.3.4-py3-none-any.whl (20 kB)\n",
      "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from requests->pipwin) (1.25.8)\n",
      "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from requests->pipwin) (3.0.4)\n",
      "Requirement already satisfied: idna<2.9,>=2.5 in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from requests->pipwin) (2.8)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from requests->pipwin) (2019.11.28)\n",
      "Requirement already satisfied: soupsieve>1.2; python_version >= \"3.0\" in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from beautifulsoup4>=4.9.0->pipwin) (1.9.5)\n",
      "Collecting pyjsparser>=2.5.1\n",
      "  Downloading pyjsparser-2.7.1.tar.gz (24 kB)\n",
      "Collecting tzlocal>=1.2\n",
      "  Downloading tzlocal-2.1-py2.py3-none-any.whl (16 kB)\n",
      "Requirement already satisfied: pyparsing>=2.0.2 in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from packaging->pipwin) (2.4.6)\n",
      "Requirement already satisfied: pytz in c:\\users\\rahul\\anaconda3\\lib\\site-packages (from tzlocal>=1.2->js2py->pipwin) (2019.3)\n",
      "Building wheels for collected packages: pipwin, docopt, pyjsparser\n",
      "  Building wheel for pipwin (setup.py): started\n",
      "  Building wheel for pipwin (setup.py): finished with status 'done'\n",
      "  Created wheel for pipwin: filename=pipwin-0.5.1-py2.py3-none-any.whl size=8783 sha256=72220b85492ad8674683bee81e780c4ff38f73ba3a9bae172289b2b22dabdaee\n",
      "  Stored in directory: c:\\users\\rahul\\appdata\\local\\pip\\cache\\wheels\\4e\\f2\\06\\9024048eece9a22afc7a013395d6de2d345f56185095550d4c\n",
      "  Building wheel for docopt (setup.py): started\n",
      "  Building wheel for docopt (setup.py): finished with status 'done'\n",
      "  Created wheel for docopt: filename=docopt-0.6.2-py2.py3-none-any.whl size=13709 sha256=af55ad2f16a04123dbf49284d77d1f9ed8c0d974616f6f1db2ed167ef5e77015\n",
      "  Stored in directory: c:\\users\\rahul\\appdata\\local\\pip\\cache\\wheels\\72\\b0\\3f\\1d95f96ff986c7dfffe46ce2be4062f38ebd04b506c77c81b9\n",
      "  Building wheel for pyjsparser (setup.py): started\n",
      "  Building wheel for pyjsparser (setup.py): finished with status 'done'\n",
      "  Created wheel for pyjsparser: filename=pyjsparser-2.7.1-py3-none-any.whl size=26004 sha256=dfb3ade75878c2368af5c166eedc46fe627bfc86311eb73d9377aeb0f3ea02b1\n",
      "  Stored in directory: c:\\users\\rahul\\appdata\\local\\pip\\cache\\wheels\\2a\\80\\ac\\dcd2bdbd03dd2b7b7e2bf3e5afbda6a1ab7935bbce314969da\n",
      "Successfully built pipwin docopt pyjsparser\n",
      "Installing collected packages: docopt, pyprind, beautifulsoup4, pyjsparser, tzlocal, js2py, pySmartDL, pipwin\n",
      "  Attempting uninstall: beautifulsoup4\n",
      "    Found existing installation: beautifulsoup4 4.8.2\n",
      "    Uninstalling beautifulsoup4-4.8.2:\n",
      "      Successfully uninstalled beautifulsoup4-4.8.2\n",
      "Successfully installed beautifulsoup4-4.9.3 docopt-0.6.2 js2py-0.71 pipwin-0.5.1 pySmartDL-1.3.4 pyjsparser-2.7.1 pyprind-2.11.3 tzlocal-2.1\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install pipwin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Below are a few links that can give details about the PyAudio class we will be using to read direct microphone input into the Jupyter Notebook\n",
    "# https://anaconda.org/anaconda/pyaudio\n",
    "# https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio\n",
    "# To install PyAudio, Run in the Anaconda Terminal CMD: conda install -c anaconda pyaudio\n",
    "# Pre-requisite for running PyAudio installation - Microsoft Visual C++ 14.0 or greater will be required. Get it with \"Microsoft C++ Build Tools\" : https://visualstudio.microsoft.com/visual-cpp-build-tools/\n",
    "# To run PyAudio on Colab, please install PyAudio.whl in your local system and give that path to colab for installation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pyaudio in c:\\users\\rahul\\anaconda3\\lib\\site-packages (0.2.11)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install pyaudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Microsoft Sound Mapper - Input',\n",
       " 'Microphone (Realtek High Defini',\n",
       " 'Microsoft Sound Mapper - Output',\n",
       " 'Speakers (Realtek High Definiti',\n",
       " 'Stereo Mix (Realtek HD Audio Stereo input)',\n",
       " 'Microphone (Realtek HD Audio Mic input)',\n",
       " 'Speakers (Realtek HD Audio output)']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import speech_recognition as speech_recog\n",
    "# Creating a recording object to store input\n",
    "rec = speech_recog.Recognizer()\n",
    "# Importing the microphone class to check availabiity of microphones\n",
    "mic_test = speech_recog.Microphone()\n",
    "# List the available microphones\n",
    "speech_recog.Microphone.list_microphone_names()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reach the Microphone and say something!\n"
     ]
    }
   ],
   "source": [
    "# We will now directly use the microphone module to capture voice input\n",
    "# Specifying the second microphone to be used for a duration of 3 seconds\n",
    "# The algorithm will also adjust given input and clear it of any ambient noise\n",
    "with speech_recog.Microphone(device_index=1) as source: \n",
    "    rec.adjust_for_ambient_noise(source, duration=3)\n",
    "    print(\"Reach the Microphone and say something!\")\n",
    "    audio = rec.listen(source)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I think you said: \n",
      "hi hello hello hello\n"
     ]
    }
   ],
   "source": [
    "# Use the recognize function to transcribe spoken words to text\n",
    "try:\n",
    "    print(\"I think you said: \\n\" + rec.recognize_google(audio))\n",
    "except Exception as e:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
